﻿package com.common.utils
{
	import com.global.GlobalDef;
	
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	import flash.utils.escapeMultiByte;

    /**
     * debug 輸出類
     * @author lxh
     * 
     */	
    public class Debug 
    {
		public static var LEVEL_INFO : int = 0;
		public static var LEVEL_DEBUG : int = 1;
		public static var LEVEL_WARNING : int = 2;
		public static var LEVEL_ERROR : int = 3;
		public static var LEVEL_SILENT : int = 4;
		
		private static var level_names : Array = ["INFO", "DEBUG", "WARNING", "ERROR", "SILENT"];
        /**
         *記錄 
         */    	
        public static const LOG:String = "log";
        /**
         *打印 
         */        
        public static const TRACER:String = "tracer";
		/**
		 *日志集合 
		 */		
		private static var logs :Array = [];
        /**
         * 輸出debug信息
         * @param contents 內容
         * @param type  類型
         */        
        public static function echo(contents:Object, type:String=TRACER):void
        {
			//return;
            switch (type)
            {
                case TRACER:
                {
					trace(contents);
                    break;
                }
                case LOG:
                {
					log(contents);
                	break;
                }
            }
        }
		/**
		 * FATAL      0  
	  	  ERROR      3  
	  	  WARN       4  
	      INFO       6  
	  	  DEBUG      7 
		 * @param contents
		 */		
		public static function log(contents:Object,type:int = 3):void
		{
			//logs.push(contents);
			return;
			var urlLoader :URLLoader = new URLLoader();
			var urlReq :URLRequest = new URLRequest();
			var urlVars :URLVariables = new URLVariables();
			urlVars.type = type;
			urlVars.info = contents + "";
			urlReq.url = "http://192.168.1.205:8090/Test/LogError.jsp";
			urlReq.method = URLRequestMethod.POST;
			urlReq.data = urlVars;
			urlLoader.load(urlReq);
			urlLoader.addEventListener(IOErrorEvent.IO_ERROR,logError);
			urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR,logError);
			function logError(e :Event):void
			{
				trace(e);
			}
		}
		/**
		 *log game history 
		 * @param uid
		 * @param matchId
		 * @param matchInfo
		 * 
		 */		
		public static function logGameHistory(uid :int,
											  matchId :int,
											  matchInfo :String):void
		{
			var urlLoader :URLLoader = new URLLoader();
			var urlReq :URLRequest = new URLRequest();
			var urlVars :URLVariables = new URLVariables();
			urlVars.uid = uid;
			urlVars.matchId = matchId;
			urlVars.info = matchInfo + "";
			urlReq.url = "http://192.168.1.205:8090/Test/LogHistory.jsp";
			urlReq.method = URLRequestMethod.POST;
			urlReq.data = urlVars;
			urlLoader.load(urlReq);
			urlLoader.addEventListener(IOErrorEvent.IO_ERROR,logError);
			urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR,logError);
			function logError(e :Event):void
			{
				trace(e);
			}
		}
		/**
		 * log error  php api
		 * @param contents
		 * @param type
		 * 
		 */		
		public static function logError(uid :int,key :String,contents:Object,type:int = 3):void
		{
			//logs.push(contents);
			//return;
			var urlLoader :URLLoader = new URLLoader();
			var urlReq :URLRequest = new URLRequest();
			var urlVars :URLVariables = new URLVariables();
			urlVars.uid = uid;
			urlVars.sid = key;
			urlVars.method = "error.postErrorLog";
			urlVars.format = "json";
			urlVars.data = contents;
			urlReq.url = GlobalDef.Web_Api_Url;
			urlReq.method = URLRequestMethod.POST;
			urlReq.data = urlVars;
			///
			try
			{
				urlLoader.load(urlReq);
			}catch(e :Error)
			{
				trace("log Error...",e);
			}
		}
		/**
		 * log bet  php api
		 * @param contents
		 * @param type
		 * 
		 */		
		public static function logBet(uid :int,key :String,contents:Object,type:int = 3):void
		{
			//logs.push(contents);
			//return;
			var urlLoader :URLLoader = new URLLoader();
			var urlReq :URLRequest = new URLRequest();
			var urlVars :URLVariables = new URLVariables();
			urlVars.uid = uid;
			urlVars.sid = key;
			urlVars.method = "game.postBetLog";
			urlVars.format = "json";
			urlVars.data = contents;
			urlReq.url = GlobalDef.Web_Api_Url;
			urlReq.method = URLRequestMethod.POST;
			urlReq.data = urlVars;
			///
			try
			{
				urlLoader.load(urlReq);
			}catch(e :Error)
			{
				trace("log Error...",e);
			}
		}
    }
}
